<template>
  <div class="management-user views-container">
    <!-- 弹窗二维码 -->
    <el-dialog
      :visible.sync="dialogVisibleImg"
      width="33%"
      custom-class="reset-pwd wlm-form-dialog dialog-table"
      :before-close="binimgColse"
    >
      <div class="wlm-form">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">页面推广</span>
        </div>
        <div
          class="flex-col flex-align-c flex-justify-c"
          style="margin:20px 0"
          v-loading="bindQrcodeImgLoading"
        >
          <img height="220" width="220" style="margin:10px 0 30px" :src="bindQrcodeImg" alt />
          <!-- <div class="flex-col"> -->
          <p style="margin-bottom:20px">微信扫一扫绑定用户</p>
          <!-- </div> -->
        </div>
      </div>
    </el-dialog>

    <!-- 弹窗用户信息 -->
    <el-dialog
      :visible.sync="dialogVisibleWechat"
      width="25%"
      custom-class="reset-pwd wlm-form-dialog dialog-table"
      :before-close="binimgColse"
    >
      <section v-loading="bindQrcodeImgLoading">
        <div class="wlm-form">
          <div class="wlm-form-header flex-row flex-justify-b">
            <span>绑定微信信息</span>
          </div>
          <div class="orbox flex-row">
            <div class="flex-row">
              <img width="50" height="50" :src="userShow.headimgurl" style="margin-right:16px;" />
              <div>
                <p style="line-height:1.8">{{userShow.nickname}}</p>
                <svg-icon
                  v-if="userShow.sex === 1"
                  style="font-size:20px;vertical-align: middle;"
                  icon-class="man"
                />
                <svg-icon
                  v-if="userShow.sex !== 1"
                  style="font-size:20px;vertical-align: middle;"
                  icon-class="woman"
                />
              </div>
            </div>
            <p
              style="line-height:1.8"
            >（{{userShow.country+'-'+userShow.province+'-'+userShow.city}}）</p>
            <el-tooltip class="item" effect="dark" content="已订阅" placement="top">
              <img
                style="float:right"
                height="20"
                width="20"
                :src="require('@/assets/image/10.png')"
                alt
              />
            </el-tooltip>
          </div>
          <div class="wlm-form-content flex-row flex-justify-b">
            <el-form style="margin-left:20px;" size="small" label-width="150px" class="retail-form">
              <el-form-item label="关注公众号时间：">
                <span>{{userShow.subscribe_time}}</span>
              </el-form-item>
              <el-form-item label="关注标识：">
                <div>
                  <el-tooltip class="item" effect="dark" content="公众号标识" placement="top">
                    <img
                      height="20"
                      width="20"
                      :src="require('@/assets/dashboard/dashbpard_wechat.png')"
                      alt
                    />
                  </el-tooltip>
                  <span>{{userShow.openid}}</span>
                </div>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
          <el-button size="small" type="primary" @click="nobind(userShow.user_id)">取消绑定</el-button>
        </footer>
      </section>
    </el-dialog>

    <el-dialog
      @close="resetDialogFormData"
      :visible.sync="dialogTableVisible"
      custom-class="reset-pwd wlm-form-dialog dialog-table"
    >
      <div class="wlm-form" v-if="dialogTableVisible">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">{{`${isFormEdit?'编辑':'添加'}用户`}}</span>
        </div>
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <div class="wlm-form-content">
            <div class="wlm-dialog-main">
              <div class="wlm-dialog-content">
                <el-form
                  ref="userForm"
                  :model="formFormatData.formData"
                  :rules="formFormatData.rules"
                  size="small"
                  label-width="120px"
                  class="retail-form"
                  label-position="right"
                >
                  <el-form-item label="登录账号" prop="user_name">
                    <el-input v-model.trim="formFormatData.formData.user_name"></el-input>
                  </el-form-item>
                  <el-form-item label="姓名" prop="real_name">
                    <el-input v-model.trim="formFormatData.formData.real_name"></el-input>
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input v-model.trim="formFormatData.formData.password"></el-input>
                  </el-form-item>
                  <el-form-item label="状态" v-if="isFormEdit">
                    <el-radio-group v-model="formFormatData.formData.is_use">
                      <el-radio :label="1">启用</el-radio>
                      <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
        <el-button size="small" type="primary" @click="formFormatSubmit">确定</el-button>
      </footer>
    </el-dialog>
    <div class="wlm-table">
      <div class="wlm-table-hearder-btn">
        <el-button size="small" type="primary" @click="dialogTableVisible=true">新增用户</el-button>
      </div>
      <div class="wlm-table-header">
        <el-form
          :model="tableFormatData.userTable.files"
          size="small"
          label-width="80px"
          class="retail-form"
          label-position="right"
        >
          <el-form-item label="关键字：" class="search">
            <el-input
              v-model="tableFormatData.userTable.files.nickname"
              placeholder="请输入账号/姓名"
              style="width:160px;"
            ></el-input>
          </el-form-item>
          <el-form-item label class="filter">
            <el-form-item label="状态：">
              <el-select v-model="tableFormatData.userTable.files.is_use" placeholder="请选择">
                <!-- <el-option v-for="item in tableFormatData.userTable.options.status" :key="item.value" :label="item.lable" :value="item.value">
                </el-option>-->
                <el-option
                  v-for="item in [{lable:'正常',value:'1'},{lable:'禁用',value:'0'}]"
                  :key="item.value"
                  :label="item.lable"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form-item>
          <el-form-item label class="filter-btns">
            <el-button v-waves type="primary" @click="filesSerch">筛选</el-button>
            <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wlm-table-content">
        <el-table
          :ref="tableFormatData.userTable.key"
          :data="tableFormatData.userTable.tableData"
          @selection-change="handleSelectionChange"
        >
          style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="date" label="姓名" align="left">
            <template slot-scope="scope">
              <span>{{scope.row.real_name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="账号" align="left">
            <template slot-scope="scope">
              <span>{{scope.row.user_name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="运营平台数" align="left">
            <template slot-scope="scope">
              <span>{{scope.row.account_count}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="注册时间" align="left">
            <template slot-scope="scope">
              <span>{{ scope.row.create_time}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="上次登录时间" align="left">
            <template slot-scope="scope">
              <span>{{ scope.row.login_time||'-'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="状态" align="left">
            <template slot-scope="scope">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                @change="changeTableItem({id:scope.row.user_id,is_use:scope.row.is_use},tableFormatData.userTable.change.status)"
                v-model="scope.row.is_use"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="300">
            <template slot-scope="scope">
              <div class="operation-group">
                <router-link
v-if="tableFormatData.userTable.otherData.oauth == 2 && scope.row.have_bind || (tableFormatData.userTable.otherData.oauth == 1 && scope.row.open_id != 0)"
                style="margin-left:10px"
tag="a"
target="_blank"
:to="{path:'/user/showBindWechatUser', query :{ 
  user_id:scope.row.user_id,user_name:scope.row.user_name,
  store_user_id:$route.query.store_user_id?$route.query.store_user_id:'',
                  h_aid:$route.query.h_aid?$route.query.h_aid:'',}}">
                <el-button
                  class="wlm-text"
                  type="text"
                >查看绑定</el-button>
                <!-- @click="showWechat(scope.row.user_id)" -->
                </router-link>
                <div v-if="tableFormatData.userTable.otherData.oauth == 2 && scope.row.have_bind || (tableFormatData.userTable.otherData.oauth == 1 && scope.row.open_id != 0)" class="btn-line"></div>
                <el-button
                  v-if="tableFormatData.userTable.otherData.oauth == 2 || (tableFormatData.userTable.otherData.oauth == 1 && scope.row.open_id == 0)"
                  class="wlm-text"
                  type="text"
                  @click="bindWechat(scope.row.user_id)"
                >绑定微信</el-button>
                <div class="btn-line"></div>
                <el-button
                  class="wlm-text"
                  type="text"
                  @click="formFormatDialogEditEvt({id:scope.row.user_id, toggle:'dialogTableVisible'})"
                >编辑</el-button>
                <div class="btn-line"></div>
                <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.user_id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-content flex-row flex-justify-b flex-align-c">
          <el-row class="pagination-btns">
            <el-checkbox
              class="check-all"
              :disabled="!hasTableData"
              true-label="1"
              false-label="0"
              v-model="tableFormatData.userTable.files.checkall"
              @change="toggleSelection"
            >全部</el-checkbox>
            <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
          </el-row>
          <el-pagination
            :disabled="!hasTableData"
            @size-change="listPageChange"
            @current-change="listPageChange"
            :current-page.sync="tableFormatData.userTable.pagination.page"
            :page-sizes="tableFormatData.userTable.pagination.pagesizes"
            :page-size.sync="tableFormatData.userTable.pagination.list_rows"
            layout="total, sizes, prev, pager, next, jumper"
            :total="hasTableData?tableFormatData.userTable.pagination.total : 0"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  memberIndex,
  memberSoftDel,
  memberStatus,
  memberDetail,
  userAdd,
  bindQrcode,
  nobindQrcode,
  showWechatUser,
  adminBindWechatResult
} from '@/api/user'
import waves from '@/directive/waves' // Waves directive
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'ManagementUser',
  components: {},
  directives: {
    waves // 点击呈现水波纹效果
  },
  computed: {},
  data() {
    return {
      // 是否显示微信用户信息
      dialogVisibleWechat: false,
      dialogTableVisible: false,
      // 是否显示绑定二维码
      dialogVisibleImg: false,
      // 绑定二维码
      bindQrcodeImg: [],
      bindQrcodeImgLoading: true,
      formFormatData: {
        key: 'userForm',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            api: memberDetail,
            params: {
              id: ''
            }
          },
          submitForm: userAdd
        },
        formData: {
          real_name: '',
          password: '',
          user_name: '',
          is_use: 1
        },
        rules: {}
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: memberIndex,
            delList: memberSoftDel
          },
          tableData: [],
          otherData: {
            oauth: ''
          },
          files: {
            nickname: '',
            is_use: '',
            ids: [],
            redirect: 'user_id',
            checkall: '0'
          },
          change: {
            status: {
              name: '编辑',
              key: 'status',
              api: memberStatus
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      },
      userShow: {},
      unBind: false
    }
  },
  created() {},
  methods: {
    nobind(user_id) {
      this.$confirm('此操作将会取消该用户的微信绑定, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          nobindQrcode({ user_id: user_id }).then((res) => {
            if (res.data.code == 1) {
              this.$message.success('取消绑定成功！')
              this.filesSerch()
            } else {
              this.$message.error('取消绑定失败！')
            }
            this.binimgColse()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    showWechat(user_id) {
      this.dialogVisibleWechat = true
      showWechatUser({ user_id: user_id }).then((res) => {
        if (res.data.code == 1) {
          this.userShow = res.data.data
          this.userShow.user_id = user_id
          this.bindQrcodeImgLoading = false
        } else {
          this.$message.error(res.data.msg)
          this.binimgColse()
        }
      })
    },
    // 关闭对话框的回调
    binimgColse() {
      this.unBind = false
      // 不显示对话框
      this.dialogVisibleImg = this.dialogVisibleWechat = false
      // 继续显示加载效果
      this.bindQrcodeImgLoading = true
    },
    // 绑定微信二维码
    bindWechat(user_id) {
      this.dialogVisibleImg = true
      bindQrcode({ user_id: user_id }).then((res) => {
        if (res.data.code == 1) {
          this.bindQrcodeImg = res.data.data.qrcode_path
          setTimeout(() => {
            this.bindQrcodeImgLoading = false
          }, 200)
          this.unBind = true
          this.bindResult(user_id)
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    // 轮询查询绑定结果
    bindResult(user_id) {
      adminBindWechatResult({ user_id: user_id }).then(res => {
        if (this.unBind == false) {
          this.binimgColse()
          return ''
        }
        if (res.data.code == 1 && res.data.data == false) {
          this.bindResult(user_id)
        } else if (res.data.data == true || res.data.code != 1) {
          this.$message.success('微信用户绑定成功~')
          this.filesSerch()
          this.binimgColse()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.management-user {
  .orbox {
    background: #ffeee6;
    padding: 20px;
    margin: 20px 20px 0;
  }
}
</style>
